package cn.xiaolang.common.utils;

import java.util.concurrent.TimeUnit;

/**
 * @author Dean
 * @date 2022/7/28
 */
public class Monitor {

    public static final int MB = 1024 * 1024;

    private static final ThreadLocal<Long> beginThread = new ThreadLocal<Long>();

    public static void begin() {
        beginThread.set(System.currentTimeMillis());
    }

    public static void end(String name) {
        double time = (System.currentTimeMillis() - beginThread.get()) / 1000.0;
        System.out.printf("%s 所用时间：%s(秒)%n", name, time);
    }

    public static Double end() {
        return (System.currentTimeMillis() - beginThread.get()) / 1000.0;
    }

    public static void printlnMemory(String tag) {
        Runtime runtime = Runtime.getRuntime();
        System.out.println("\n"+tag + ":" + runtime.freeMemory() / MB + "M(free)/" + runtime.totalMemory() / MB + "M(total)");
    }
}
